//二级城市
function selectSecondArea(obj) {
	//省ID
	var parentId = $(obj).val();
	$("#secondCity").attr("style","margin:0 0.3em;display:none;");
	$("#areaForCity").attr("style","display:none;");
	$.ajax({
		url: "weiListSecondCityByParentId.jhtml",
		type: "GET",
		data: {parentId: parentId},
		dataType: "json",
		cache: false,
		success: function(data) {
			if(JSON.stringify(data.secondCitys) != "[]") {
				var secondCityOption = '<option selected="selected" value="">选城市</option>';
				$.each(data.secondCitys, function(idx, item){
					secondCityOption += '<option  value="'+ item.id +'">'+ item.name +'</option>';
				});
				$("#secondCity").empty().append(secondCityOption);
				$("#secondCity").attr("style","margin:0 0.3em;");
			}
		}
	});
}

//三级地区
function selectSecondCity(obj) {
	//所选城市
	var secondCityId = $(obj).val();
	//将所选城市ID放入隐藏input中
	
	//获取省ID
	var parentId = $("#rootSelect").find('option:selected').val();
	$("#areaForCity").attr("style","display:none;");
	$.ajax({
		url: "weiListThreeCityByParentId.jhtml",
		type: "GET",
		data: {parentId: secondCityId},
		dataType: "json",
		cache: false,
		success: function(data) {
			if(JSON.stringify(data.areas) != "[]") {
				var areasOption = '<option selected="selected" value="">选地区</option>';
				$.each(data.areas, function(idx, item){
					areasOption += '<option  value="'+ item.id +'">'+ item.name +'</option>';
				});
				$("#areaForCity").empty().append(areasOption);
				$("#areaForCity").attr("style","");
			}
		}
	});
}

//保存填写收货信息
function saveAddressInfo() {
	var zipCodeRegx = /\d{6}/;
	//var consigneeRegx = /^[0-9a-z_A-Z\u4e00-\u9fa5]+$/;
	var phoneRegx = /\d{11}/;
	
	var zipCode = $("#zipCode").val();
	var consignee = $("#consignee").val();
	var phone = $("#phone").val();
	//省
	var province = $("#rootSelect").find("option:selected").html();
	var secondCity = $("#secondCity").find("option:selected").html();
	var areaForCity = $("#areaForCity").find("option:selected").html();
	var address = $("#address").val();
	//对邮编校验
	if(zipCode == "") {
		$.message("error", "请填写邮编!");
		return;
	}else if(!zipCodeRegx.test(zipCode)) {
		$.message("error", "请输入6位数字邮编!");
		return;
	}else{}
	//对收货人的校验
	if(consignee == "") {
		$.message("error", "请填写收货人姓名！");
		return;
	}
	//对电话号校验
	if(phone == "") {
		$.message("error", "请填写联系电话！");
		return;
	}
	if(!phoneRegx.test(phone)) {
		$.message("error", "请输入正确联系电话!");
		return;
	}
	//对省、市、区的校验
	if(province == "选省份") {
		$.message("error", "请选择省份！");
		return;
	}else {
		$("#hidden_province").attr("value", province);
	}
	if(secondCity == "选城市") {
		$.message("error", "请选择城市！");
		return;
	}else {
		$("#hidden_secondCity").attr("value", secondCity);
	}
	if(areaForCity == "选地区") {
		$.message("error", "请选择地区！");
		return;
	}else {
		$("#hidden_areaForCity").attr("value", areaForCity);
		var areaId = $("#areaForCity").find("option:selected").val();
		$("#hidden_areaId").attr("value", areaId);
	}
	//对详细地址的校验
	if(address == "") {
		$.message("error", "请输入详细地址！");
		return;
	}
	//如果没有二级城市或三级地区对areaId的处理
	if($("#areaForCity").has("option").length == 0) {
		var areaId = $("#secondCity").find("option:selected").val();
		$("#hidden_areaId").attr("value", areaId);
	}
	//异步请求对显示收货地址处刷新
	$.ajax({
		url: "weixinSaveAddress.jhtml",
		type: "POST",
		data: $("#addressInfoForm").serialize(),
		dataType: "json",
		cache: false,
		success: function(data) {
			var listAddressInfo = '<div class="userOrder" onclick="javascript:chooseAddress();">'+
				            		  '<p><b>收货人：'+ data.receiver.consignee +'</b><b>联系电话：'+ data.receiver.phone +'</b></p>'+
								      '<p><b>收货地址：'+ data.receiver.areaName +''+ data.receiver.address +'</b></p>'+
								  '</div>';
			$("#listAddressInfo").empty().append(listAddressInfo);
			$("#orderForm").append('<input type="hidden" id="receiverId" name="receiverId" value="'+ data.receiver.id +'"/>');
		}
	});
}

//跳转到更改、添加收货地址页面
function chooseAddress() {
	//alert(123456789);
	var productId = '';
	if($("#orderForm").find("input[name='productId']").length != 0) {
		productId += $("#hidden_productId").val();
	}
	var storageId = '';
	if($("#orderForm").find("input[name='storageId']").length != 0) {
		storageId += $("#hidden_storageDeliveryFeeId").val();
	}
	var receiverId = $("#receiverId").val();
	var initTotalPrice = $("#init_totalPrice").val();
	var totalPrice = $("#hidden_totalPrice").val();
	var paymentMethodId = $("#hidden_paymentMethodId").val();
	var shippingMethodId = $("#hidden_shippingMethodId").val();
	var selectedPhoneNum = $("#hidden_selectedPhoneNum").val();
	var isOpenCallsDisplay = $("#hidden_isOpenCallsDisplay").val();
	var phonePackageId = $("#hidden_phonePackageId").val();
	var userName = $("#hidden_userName").val();
	//alert(userName);
	userName = encodeURI(userName);
	userName = encodeURI(userName);
	var certNumber = $("#hidden_certNumber").val();
	var cloudServiceId = $("#hidden_cloudServiceId").val();
	var cloundServicesPhone = $("#cloundServicesPhone").val();
	var whetherInvoice = $("#whetherInvoice").val();
	
	//可选包
	var pwis = new Array();
	var i=0;
	var packageWrapItemIds = '';
	if($("#orderForm").find("input[name='packageWrapItemIds']").length != 0) {
		$("#orderForm").find("input[name='packageWrapItemIds']").each(function(idx, item) {
			pwis[i] = $(item).attr("value");
			i++;
		});
		packageWrapItemIds += pwis.toString();
	}
	
	window.location.href = "${base}/weixin/member/order/toChooseAddress.jhtml?productId="+ productId +"&receiverId="+ receiverId +"&initTotalPrice="+ initTotalPrice +"" +
	  "&totalPrice="+ totalPrice +"&paymentMethodId="+ paymentMethodId +"&shippingMethodId="+ shippingMethodId +"" +
	  "&selectedPhoneNum="+ selectedPhoneNum +"&isOpenCallsDisplay="+ isOpenCallsDisplay +"&phonePackageId="+ phonePackageId +"&userName="+ userName +"" +
	  "&certNumber="+ certNumber +"&cloudServiceId="+ cloudServiceId +"&storageId="+ storageId +"&packageWrapItemIds="+ packageWrapItemIds +"&whetherInvoice="+whetherInvoice+"&cloundServicesPhone="+cloundServicesPhone+"";

}







